using System;
using System.Collections.Generic;

namespace NDomain.Reflection
{
    public class ProjectInspector
    {
        private IProjectInfoProvider _pinfoProvider;
        private ICollection<IClassProcessor> _processors;

        public ProjectInspector(IProjectInfoProvider pinfoProvider, params IClassProcessor[] processors)
        {
            if (pinfoProvider == null)
                throw new ArgumentNullException("pinfoProvider");

            if (processors == null || processors.Length == 0)
                throw new ArgumentNullException("processors");

            _pinfoProvider = pinfoProvider;
            _processors = processors;
        }

        public DomainInfo GetDomain()
        {
            DomainInfo domainInfo = new DomainInfo(_pinfoProvider.GetProjectName());
            Process(domainInfo);
            return domainInfo;
        }

        private void Process(IDomainInfo domainInfo)
        {
            ICollection<IClassInfoProvider> classes = _pinfoProvider.GetClasses();
            if (classes.Count > 0)
            {
                foreach (IClassProcessor processor in _processors)
                    processor.InitProcessing(domainInfo);

                foreach (IClassProcessor processor in _processors)
                    foreach (IClassInfoProvider clazz in classes)
                        processor.Process(clazz, domainInfo);

                foreach (IClassProcessor processor in _processors)
                    processor.FinalizeProcessing(domainInfo);
            }
        }
    }
}